﻿using NewRetail.Application.Components.Goods.Entity;
using NewRetail.Application.Components.Platform.Entity;
using NewRetail.Application.Components.Warehouse.Entity;
using NewRetail.Application.Core;
using System;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
//add by cl 2022.12.04
namespace NewRetail.Application.Components.Warehouse
{
    public interface IDefectiveBillService : IApplicationService
    {
        Task<DefectiveBill[]> AttachWarehouse(DefectiveBill[] data);
        Task<DefectiveBill[]> AttachGoodsInfo(DefectiveBill[] data);
        Task<DefectiveBill[]> AttachSupplier(DefectiveBill[] data);
    }

    public class DefectiveBillService : MyApplicationService, IDefectiveBillService
    {
        private readonly IRepository<DefectiveBill> defectiveBills;
        public DefectiveBillService(IRepository<DefectiveBill> defectiveBills)
        {
            this.defectiveBills = defectiveBills;
        }

        public async Task<DefectiveBill[]> AttachWarehouse(DefectiveBill[] data)
        {
            var db = await this.defectiveBills.GetDbContextAsync();

            await db.AttachData<Warehouse.Entity.Warehouse, DefectiveBill>(
                data, idGetter: x => x.warehouseID, action: (m, arr) =>
                {
                    m.Warehouse = arr.FirstOrDefault();
                });

            return data;
        }
        public async Task<DefectiveBill[]> AttachGoodsInfo(DefectiveBill[] data)
        {
            var db = await this.defectiveBills.GetDbContextAsync();

            await db.AttachData<GoodsInfo, DefectiveBill>(data, x => x.goodsID, (m, arr) =>
            {
                m.GoodsInfo = arr.FirstOrDefault();
            });

            return data;
        }

        public async Task<DefectiveBill[]> AttachSupplier(DefectiveBill[] data)
        {
            var db = await this.defectiveBills.GetDbContextAsync();

            await db.AttachData<Supplier, DefectiveBill>(data, x => x.supplierID, (m, arr) =>
            {
                m.Supplier = arr.FirstOrDefault();
            });

            return data;
        }

    }
}
